package com.lzs.adx.web.modules.adx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lzs.adx.commmon.page.PageData;
import com.lzs.adx.commmon.service.impl.CrudServiceImpl;
import com.lzs.adx.web.commmon.enums.ReportEnums;
import com.lzs.adx.web.modules.adx.dao.ReportTotalDao;
import com.lzs.adx.web.modules.adx.dto.AccountReportDayDTO;
import com.lzs.adx.web.modules.adx.dto.ReportTotalDTO;
import com.lzs.adx.web.modules.adx.entity.AccountReportDayEntity;
import com.lzs.adx.web.modules.adx.entity.ReportTotalEntity;
import com.lzs.adx.web.modules.adx.service.ReportTotalService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * 报表汇总
 *
 * @author wangzhijun 825400558@qq.com
 * @since 1.0.0 2023-06-06
 */
@Service
@Slf4j
public class ReportTotalServiceImpl extends CrudServiceImpl<ReportTotalDao, ReportTotalEntity, ReportTotalDTO> implements ReportTotalService {

    @Resource
    private ReportTotalDao reportTotalDao;

    @Override
    public QueryWrapper<ReportTotalEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String stime = (String)params.get("stime");
        String etime = (String)params.get("etime");
        String dspId = (String) params.get("dspId");
        String date = (String) params.get("date");




//        ReportEnums reportEnums = ReportEnums.getReportEnums(type);

        QueryWrapper<ReportTotalEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        wrapper.ge(StringUtils.isNotBlank(stime) ,"date",stime);
        wrapper.le(StringUtils.isNotBlank(etime) ,"date",etime);
        wrapper.eq(StringUtils.isNotBlank(dspId) ,"dsp_id",dspId);
        wrapper.eq(StringUtils.isNotBlank(date) ,"date",date);


//        wrapper.eq(Objects.nonNull(reportEnums) ,reportEnums.getColums(),type);


        wrapper.orderByDesc("date").orderByDesc("result_type_total");

        return wrapper;
    }




    public PageData<ReportTotalDTO> selectReportTotalPage(Map<String, Object> params) {
        IPage<ReportTotalDTO> listPage = reportTotalDao.selectReportTotalPage(getPage(params, null, false), params);

        return new PageData<>(listPage.getRecords(), listPage.getTotal());
    }

    @Override
    public PageData<ReportTotalDTO> selectOcpxReportTotalPage(Map<String, Object> params) {
        IPage<ReportTotalDTO> listPage = reportTotalDao.selectOcpxReportTotalPage(getPage(params, null, false), params);

        return new PageData<>(listPage.getRecords(), listPage.getTotal());
    }


    @Override
    public void batch(ReportTotalEntity reportTotalEntity) {
        if(Objects.isNull(reportTotalEntity) || ObjectUtils.isEmpty(reportTotalEntity.getDspId())){
            return;
        }
        log.info("请求的参数为{}",reportTotalEntity);
        synchronized (reportTotalEntity.getDspId()) {

            LambdaQueryWrapper<ReportTotalEntity> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(ReportTotalEntity::getDate, reportTotalEntity.getDate());
            lambdaQueryWrapper.eq(ReportTotalEntity::getDspId, reportTotalEntity.getDspId());

            ReportTotalEntity reportTotal = reportTotalDao.selectOne(lambdaQueryWrapper);

            log.info("查询的对象{}", JSONObject.toJSONString(reportTotal));
            if (Objects.isNull(reportTotal)) {
                reportTotalDao.insert(reportTotalEntity);
            } else {
                reportTotal.setCpdTotal(reportTotalEntity.getCpdTotal());
                reportTotal.setCpcTotal(reportTotalEntity.getCpcTotal());
                reportTotal.setImpTotal(reportTotalEntity.getImpTotal());
                reportTotal.setResultTypeTotal(reportTotalEntity.getResultTypeTotal());
                reportTotal.setRegister(reportTotalEntity.getRegister());
                reportTotal.setAct(reportTotalEntity.getAct());
                reportTotal.setRetain(reportTotalEntity.getRetain());
                reportTotal.setPay(reportTotalEntity.getPay());
                reportTotal.setWakeup(reportTotalEntity.getWakeup());
                reportTotal.setKeybehaviors(reportTotalEntity.getKeybehaviors());
                reportTotal.setRed(reportTotalEntity.getRed());
                reportTotal.setHprofit(reportTotalEntity.getHprofit());
                reportTotal.setType(reportTotalEntity.getType());
                reportTotalDao.updateById(reportTotal);
            }
        }
    }

    @Override
    public ReportTotalDTO getDetail(String dspId, String day) {
        LambdaQueryWrapper<ReportTotalEntity> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ReportTotalEntity::getDspId,dspId);
        lambdaQueryWrapper.eq(ReportTotalEntity::getDate,day);
        ReportTotalDTO reportTotalDTO=new ReportTotalDTO();
        ReportTotalEntity reportTotal = this.baseDao.selectOne(lambdaQueryWrapper);
        if(Objects.isNull(reportTotal)){
            return null;
        }
        BeanUtils.copyProperties(reportTotal,reportTotalDTO);
        return reportTotalDTO;
    }


}